# -*- encoding: utf-8 -*-
#
# Lúcuma
# Copyright © 2008 Diego Escalante Urrelo  <diegoe@gnome.org>
#
# Licensed under the GNU General Public License Version 2
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor
# Boston, MA  02110-1301, USA.
#

from datetime import datetime

from django.http import HttpResponse, HttpResponseRedirect

from lucuma.tasks.models import Task
from lucuma.tasks.forms import InlineTaskForm, EditTaskForm
from lucuma.personas.models import Persona
from lucuma.notes.models import Note
from lucuma.tags.models import _add_tags_to_obj

def add(request):
    """
    Add a new Task.
    """
    if not request.session.get('logged', False):
        return HttpResponseRedirect('/login')
    
    if request.method != 'POST':
        return HttpResponseRedirect('/tasks')

    form = InlineTaskForm(request.POST)
    
    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/tasks')
    else:
        return HttpResponseRedirect('/tasks')

def edit(request, task_id):
    """
    Edit a Task.
    """
    if not request.session.get('logged', False):
        return HttpResponseRedirect('/login')
    
    if request.method != 'POST':
        return HttpResponseRedirect('/tasks')

    task = Task.objects.get(pk=task_id)
    form = EditTaskForm(request.POST, instance=task)
    
    if form.is_valid():
        form.save()

    return HttpResponseRedirect('/tasks/%d' % task.id)
    

def delete(request, task_id):
    """
    Delete a Task.
    """
    if not request.session.get('logged', False):
        return HttpResponseRedirect('/login')
    
    task = Task.objects.get(pk=task_id)
    
    if not task:
        return HttpResponseRedirect('/tasks/')
    
    task.delete()
    
    return HttpResponseRedirect('/tasks/')

def __status(request, task_id, done):
    """
    Toggles the Task task_id done state.
    """
    if not request.session.get('logged', False):
        # FIXME: this will NOT make the ajax fail
        #return HttpResponseRedirect('/login')
        # FIXME: for testing
        pass

    task = Task.objects.get(pk=task_id)
    
    if not task:
        return HttpResponseRedirect('/tasks/')
    
    task.done = done
    task.save()
    
    return HttpResponse('0')

# FIXME: should be something smarter
def status_done(request, task_id):
    """
    Marks a Task as done.
    """
    return __status(request, task_id, True)

# FIXME: should be something smarter
def status_undone(request, task_id):
    """
    Marks a Task as pending.
    """
    return __status(request, task_id, False)
